﻿#region <!-- Using Directives -->

using System;
using System.Collections.Generic;
using System.Xml;

using WordSharpDev.Extensions;

#endregion

namespace WordSharpDev.Utils
{
	internal class ErrException : Exception
	{
		#region <!-- ErrException -->

		public ErrException(String p_Message) : base(p_Message)
		{
		}

		public ErrException(String p_Message, Exception p_InnerException) : base(p_Message, p_InnerException)
		{
		}

		#endregion

		#region <!-- ChildExceptions -->

		protected readonly List<Exception> m_ChildExceptions = new List<Exception>();

		public List<Exception> ChildExceptions { get { return m_ChildExceptions; } }

		public void AppendChildException(Exception p_Exception)
		{
			m_ChildExceptions.Add(p_Exception);
		}

		public void AppendChildException(String p_Message)
		{
			m_ChildExceptions.Add(new Exception(p_Message));
		}

		#endregion

		#region <!-- Parameters -->

		protected readonly Dictionary<String, String> m_Parameters = new Dictionary<String, String>();

		public Dictionary<String, String> Parameters { get { return m_Parameters; } }

		public void AppendParameter(String p_Name, String p_Value)
		{
			if (m_Parameters.ContainsKey(p_Name))
				m_Parameters.Remove(p_Name);

			m_Parameters.Add(p_Name, p_Value);
		}

		#endregion

		#region <!-- ToXmlNode -->

		public XmlNode ToXmlNode()
		{
			var l_Node = "<Exception />".AsXmlNode();

			var l_Params = String.Format("{0}\r\n{1}", "Type", GetType().Name);

			foreach (var i_ParamName in Parameters.Keys)
			{
				l_Params += "\r\n" + String.Format("{0}\r\n{1}", i_ParamName, Parameters[i_ParamName]);
			}

			l_Node.GetNewChild("Params").AppendData(l_Params);

			l_Node.GetNewChild("Message").AppendData(Message);

			if (InnerException != null)
			{
				l_Node.GetNewChild("Exception").InnerXml = InnerException.ToXmlNode().InnerXml;
			}

			foreach (var i_Exception in ChildExceptions)
			{
				l_Node.GetNewChild("Exception").InnerXml = i_Exception.ToXmlNode().InnerXml;
			}

			return l_Node;
		}

		#endregion

		#region <!-- ToString -->

		public override String ToString()
		{
			return ToXmlNode().OuterXml.FormatXml();
		}

		#endregion
	}
}